package com.xqls.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class GetKeyByValue {

	  //根据value值获取到对应的一个key值  
	    public static int getKey(Map<Integer,String> map,String value){  
	        int key = 0;  
	       //Map,HashMap并没有实现Iteratable接口.不能用于增强for循环.  
	         for(Integer getKey: map.keySet()){  
	            if(map.get(getKey).equals(value)){  
	                key = getKey;  
	            }
	         }
         return key;  
	     //这个key肯定是最后一个满足该条件的key.  
    }  
	    
	    /**
	     * 根据这个月日期，求上个月当天日期
	     * @param listMap
	     * @return
	     * @throws ParseException
	     */
	    public static List<Map<String, Object>> getLastMonthByThisMonth(List<Map<String, Object>> listMap) throws ParseException{
			Calendar calendar = Calendar.getInstance();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
			Date lastMonth = null;
			for(int i = 0;i<listMap.size();i++){
				calendar.setTime(format1.parse(listMap.get(i).get("thisMonth").toString()));
				calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
				lastMonth = calendar.getTime();
				listMap.get(i).put("lastMonth", format.format(lastMonth));
			}
			return listMap;
		}
}
